package com.smart.manager.pop.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.option.SelectOption;
import com.smart.manager.pop.entities.body.RPopBasicLink;
import com.smart.manager.pop.entities.models.PopBasicLink;
import com.smart.manager.pop.entities.models.query.QPopBasicLink;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 支付中心-订单管理
 *
 * @author Administrator
 */

@Log4j2
@Service
@RequiredArgsConstructor
public class PopLinkRepository {


    public Page<PopBasicLink> pageList(RPopBasicLink.SearchBody search) {

        QPopBasicLink query = new QPopBasicLink();
        Opt.ofBlankAble(search.getTitle()).ifPresent(x->query.title.contains(x)) ;
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows()).order().id.desc();
        return new Page<PopBasicLink>().load(query.findPagedList());
    }

    /**
     * 添加
     *
     * @param form 形式
     */
    public void add(RPopBasicLink.EditBody form) {
        PopBasicLink record = BeanUtil.copyProperties(form, PopBasicLink.class);
        record.save();

    }

    /**
     * 更新
     *
     * @param id   id
     * @param form 形式
     */
    public void update(Long id, RPopBasicLink.EditBody form) {
        PopBasicLink record = new QPopBasicLink().id.eq(id).findOne();
        Assert.notNull(record, "活动链接不存在");
        BeanUtil.copyProperties(form, record);
        record.update();


    }

    /**
     * 批量删除
     *
     * @param idList id列表
     * @return int
     */
    public int delByIds(List<Long> idList) {
        return DB.deleteAll(PopBasicLink.class, idList);
    }


    public List<SelectOption> getOptions() {
        QPopBasicLink place = new QPopBasicLink();
        return place.findStream().map(x ->
                        new SelectOption().setLabel(x.getTitle()).setValue(String.valueOf(x.getId()))
                )
                .collect(Collectors.toList());
    }
}
